package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.MenuBO;
import com.lwc.cfdns.pojo.dto.system.MenuDTO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.entity.SystemMenu;
import com.lwc.cfdns.pojo.vo.system.MenuTreeVO;
import com.lwc.cfdns.pojo.vo.system.MenuVO;
import com.lwc.cfdns.service.impl.SystemMenuServiceImpl;
import com.lwc.cfdns.service.impl.SystemRoleMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author luwc
 * @title MenuController
 * @description
 * @time 2023/5/4 22:55
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class MenuController {
    @Autowired
    private SystemMenuServiceImpl systemMenuService;

    @Autowired
    private SystemRoleMenuServiceImpl systemRoleMenuService;

    /**
     * 创建
     *
     * @param
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/4 23:06
     */
    @PreAuthorize("hasAnyAuthority('system:menu:create')")// 权限控制
    @PostMapping("/menu")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody MenuDTO menuDTO) {
        return systemMenuService.create(menuDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/4 23:06
     */
    @PreAuthorize("hasAnyAuthority('system:menu:view')")// 权限控制
    @GetMapping("/menu/{id}")
    public MenuVO view(@PathVariable("id") @NotEmpty(message = "菜单ID不能为空") @NotBlank(message = "菜单ID不能为空!") @Min(value = 3, message = "最小值必须为1") Long id) {
        return systemMenuService.view(id);
    }

    /**
     * 更新
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/4 23:07
     */
    @PreAuthorize("hasAnyAuthority('system:menu:update')")// 权限控制
    @PutMapping("/menu/{id}")
    public Map<String, Object> update(@Validated(ValidGroup.Update.class) @RequestBody MenuDTO menuDTO, @PathVariable("id") @NotEmpty(message = "菜单ID不能为空") Long id, HttpServletRequest request) {
        System.out.println(request.getRequestURI());
        return systemMenuService.update(id, menuDTO);
    }

    /**
     * 删除
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/4 23:07
     */
    @PreAuthorize("hasAnyAuthority('system:menu:delete')")// 权限控制
    @DeleteMapping("/menu/{id}")
    public Map<String, Object> delete(@PathVariable("id") @NotEmpty(message = "菜单ID不能为空") Long id) {
        return systemMenuService.delete(id);
    }

    /**
     * 树型列表
     *
     * @param menuBO
     * @return List<SystemMenu>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/8 20:38:33
     */
    @PreAuthorize("hasAnyAuthority('system:menu:lists')")// 权限控制
    @GetMapping("/menu")
    public IPage<SystemMenu> lists(MenuBO menuBO) {
        menuBO.setPageSize(1000);
        return systemMenuService.lists(menuBO);
    }

    /**
     * 下拉树
     *
     * @param menuBO
     * @return List<SystemMenu>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/9 08:40:41
     */
    @GetMapping("/menu/tree")
    public List<MenuTreeVO> tree(MenuBO menuBO) {
        menuBO.setStatus(0);
        menuBO.setVisible(0);
        return systemMenuService.tree(menuBO);
    }

    /**
     * 获取角色的已选中菜单
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:33
     */
    @GetMapping("/menu/roleMenuTreeselect/{id}")
    public Map<String, Object> roleMenuTreeselect(@PathVariable("id") @NotNull(message = "角色ID不能为空") Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("menus", tree(new MenuBO()));
        map.put("checkedKeys", systemRoleMenuService.roleMenuTreeselect(id));
        return map;
    }
}
